package com.tedu.movie.service;

import com.tedu.movie.mapper.ItemMapper;
import com.tedu.movie.mapper.TicketMapper;
import com.tedu.movie.pojo.Item;
import com.tedu.movie.pojo.Ticket;
import com.tedu.movie.pojo.TicketExample;
import com.tedu.movie.pojo.TicketItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TicketServiceImpl implements TicketService{
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    TicketMapper ticketMapper;
    @Override
    public List<TicketItemVO> selectAll() {
        ArrayList<TicketItemVO> volist=new ArrayList<>();
        List<Ticket> list = ticketMapper.selectByExample(null);
        for(Ticket ticket:list){
            Item item = itemMapper.selectByPrimaryKey(Integer.valueOf(ticket.getCinema()));
            TicketItemVO ticketItemVO=new TicketItemVO();
            ticketItemVO.setCinema(ticket.getCinema());
            ticketItemVO.setId(ticket.getId());
            ticketItemVO.setStatus(ticket.getStatus());

            ticketItemVO.setName(item.getName());
            ticketItemVO.setImage(item.getImage());
            ticketItemVO.setSeat(item.getSeat());

            volist.add(ticketItemVO);
        }
        return volist;
    }

    @Override
    public List<Ticket> selectByCinemaId(String id) {
        TicketExample ticketExample=new TicketExample();
        TicketExample.Criteria criteria=ticketExample.or();
        criteria.andCinemaEqualTo(id);
        List<Ticket> list =ticketMapper.selectByExample(ticketExample);
        return list;
    }

    @Override
    public Ticket selectById(String id) {
        Ticket ticket=ticketMapper.selectByPrimaryKey(id);
        return ticket;
    }

    @Override
    public boolean insertTicket(Ticket ticket) {
        int insertRow=ticketMapper.insertSelective(ticket);
        if(insertRow>=1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteTicket(String id) {
        int deleteRow =ticketMapper.deleteByPrimaryKey(id);
        if(deleteRow>=1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateTicket(Ticket ticket) {
        int updateRow=ticketMapper.updateByPrimaryKeySelective(ticket);
        if(updateRow>=1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Ticket> selectByStatus(boolean status) {
        TicketExample ticketExample=new TicketExample();
        TicketExample.Criteria criteria=ticketExample.or();
        criteria.andStatusEqualTo(status);
        List<Ticket> list=ticketMapper.selectByExample(ticketExample);
        return list;
    }
}
